package mymac.com.app;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

/**
 * Created by mymac on 2017/4/10.
 */

public class TestActivity extends AppCompatActivity implements DialogInterface.OnClickListener{
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;
    private String[] mLanguage = new String[]{"简体中文","English"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
    }

    private void initData() {
        String isFirstOpen = mSharedPreferences.getString("isFirstOpen","");
        if(isFirstOpen.equals("")){
            ShowDialog();
            mEditor.putString("isFirstOpen","0");
            mEditor.commit();
        } else {
            Toast.makeText(this, "您上一次已选择语言,请点击下一步", Toast.LENGTH_SHORT).show();
        }

    }

    private void init() {
        mSharedPreferences = getSharedPreferences("TEST", Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    private void ShowDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择语言");
        builder.setItems(mLanguage, this);
        builder.setNegativeButton("Cancel",null);
        builder.show();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(this, "您已选择" + mLanguage[which], Toast.LENGTH_SHORT).show();
    }
}
